package pl.cba.solgar.game.rogal.commands;

import java.util.Iterator;

import org.newdawn.slick.Color;

import pl.cba.solgar.game.rogal.CommandRecognizer;
import pl.cba.solgar.game.rogal.MessageConsole;
import pl.cba.solgar.game.rogal.Printable;
import pl.cba.solgar.game.rogal.Rogal;
import pl.cba.solgar.game.rogal.console.Console;
import pl.cba.solgar.game.rogal.utils.BinarySearchTree;

public class MessagesHistoryCommand extends RogalCmd implements Printable {

	private MessagesHistoryCommand instance;
	private int offset;
	private boolean canScrolUp = false;
	private boolean canScrolDown = false;

	public MessagesHistoryCommand(String keys, Rogal rogal, CommandRecognizer crec) {
		super(keys, rogal);
		this.crec = crec;
		instance = this;
		commands = new BinarySearchTree();
		crec.addCommand(new ScrolDown(), commands);
		crec.addCommand(new ScrolUp(), commands);
		crec.addCommand(new Exit(), commands);
	}

	public boolean execute() {
		reset();
		crec.setCurrentDefaultCommand(new Command("") { public boolean execute() { return false; }});
		rogal.registerConsolePrintable(this);
		crec.setCurrentTree(commands);
		offset = 0;
		determineIfCanScrolDown();
		canScrolUp = false;
		return false;
	}

	public String getShortDescription() {
		return "shows scrolable messages history";
	}

	public void print(Console c) {
		c.clear();
		MessageConsole cons = MessageConsole.getInstance();
		Iterator<MessageConsole.Message> iter = cons.getIterator();
		int line = 0;
		int i = 0;
		while (iter.hasNext()) {
			MessageConsole.Message msg = iter.next();
			if (i >= offset) {
				c.print(msg.msg + (msg.times > 1 ? " [x"+msg.times+"]" : ""), 0, line, Color.white, null);
				line += 1;
			}
			i += 1;
			if (line >= Rogal.CONSOLE_ROWS - 1)
				break;
		}
		String txt = "";
		if (canScrolUp)
			txt += "[-] to scrol up ";
		if (canScrolDown)
			txt += "[+] to scrol down ";
		txt += "[z] to exit";

		c.setColor(Color.yellow);
		c.print(txt, Rogal.CONSOLE_COLS/2 - txt.length()/2, Rogal.CONSOLE_ROWS - 1);
	}

	private void determineIfCanScrolDown() {
		int lines = MessageConsole.getInstance().lines();
		System.out.println("lines - offset: " + (lines - offset));
		if(lines - offset > Rogal.CONSOLE_ROWS - 1) {
			canScrolDown = true;
		}
		else {
			canScrolDown = false;
		}
	}

	private class ScrolDown extends Command {

		public ScrolDown() { super("+"); }

		public boolean execute() {
			int lines = MessageConsole.getInstance().lines();
			if(lines - offset > Rogal.CONSOLE_ROWS - 1) {
				offset += 1;
				canScrolUp = true;
			}
			determineIfCanScrolDown();
			return false;
		}

	}

	private class Exit extends Command {

		public Exit() { super("z"); }

		public boolean execute() {
			rogal.unregisterConsolePrintable(instance);
			return true;
		}

	}

	private class ScrolUp extends Command {

		public ScrolUp() { super("-"); }

		public boolean execute() {
			offset -= 1;
			if (offset < 0)
				offset = 0;

			if (offset == 0)
				canScrolUp = false;
			else
				canScrolUp = true;

			determineIfCanScrolDown();

			return false;
		}

	}

}
